package com.jframe.sso.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.sso.model.Role;
import com.jframe.sso.model.UserGroup;
import com.jframe.sso.service.RoleService;
import com.jframe.sso.service.UserGroupService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.controller.utils.TabmCache;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class RoleAction extends ExtendAction{
	
	private static String ACTION="roleAction";
	private static String SERVICE="roleService";
	private static Class<Role> CLAZZ=Role.class;
	
	public void list(){

		//加载数据字典
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();
		frozenColumns.add(new Columns("ck","",40,""));
		commonColumns.add(new Columns("id", "编号", 100,""));
		commonColumns.add(new Columns("code", "权限代码", 100,""));
		commonColumns.add(new Columns("name","名称",120,""));
		commonColumns.add(new Columns("upId","父角色",120,new Editor("combobox",true),TabmCache.getCombotreeJson("sm_sys_role")));
		commonColumns.add(new Columns("meno","描述",150,""));
		//commonColumns.add(new Columns("groupId","所属用户组",100,new Editor("combobox",true),TabmCache.getComboboxJson("sm_sys_usergroup",true)));
		commonColumns.add(new Columns("active","有效否",100,new Editor("combobox",true),DicCache.getComboboxJson("dm_sys_active")));
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=adminload"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/jframe/sso/list/roleList.jsp");
	}
	
	public void adminload(){
		RoleService roleService = (RoleService)this.getBean("roleService");
		//获取当前role
		Role role = roleService.load(this.getCurUser().getRoleId());	
		//授权判断，可扩展
		if(role.getGroupId()==1||role.getGroupId()==2){
			this.baseLoad(SERVICE, CLAZZ);
		}else{
			System.out.println("未经授权非法查看，自动退出！");
			this.forward("/loginAction.action?method=loginOut");
		}
	}
	
	//普通用户管理员
	public void commonload(){
		RoleService roleService = (RoleService)this.getBean("roleService");
		//获取当前角色role
		Role role = roleService.load(this.getCurUser().getRoleId());
		this.baseLoad(SERVICE, CLAZZ," and groupId=? and upId=? ","", new Object[]{role.getGroupId(),role.getId()});
	}		
	
	public void save(){
		this.baseSave(SERVICE, CLAZZ);
	}
	
	public Object beforeSave(Object object){
		String type = this.request.getParameter("type");
		Role role = (Role)object;
		if(type.equals("add")){
			int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
			role.setGroupId(groupId);
		}
		return role;
	}
	
	public void afterSave(Object object){
		TabmCache.reload("sm_sys_bmrole");
		TabmCache.reload("sm_sys_role");
	}
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void afterDelete(String ids){
		TabmCache.reload("sm_sys_bmrole");
		TabmCache.reload("sm_sys_role");
	}
	
	public List<UserGroup> getUserGroups(){
		UserGroupService userGroupService = (UserGroupService)this.getBean("userGroupService");
		List<UserGroup> ls = userGroupService.findList("where active=1");
		return ls;
	}
	
	public String getUpData(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		RoleService roleService = (RoleService)this.getBean("roleService");
		List<Role> ls = roleService.findByGroupId(groupId);
		String json = "[";
		for(Role role:ls){
			json +="{'id':"+role.getId()+",";
			json +="'name':'"+role.getName()+"'},";
		}
		//去除尾部逗号
		if(ls.size()>0){
			json = json.substring(0,json.length()-1);
		}
		json += "]";
		return json;
	}
	
	public void getGroups(){
		UserGroupService userGroupService = (UserGroupService)SpringUtil.getBean("userGroupService");
		List<UserGroup> ls = userGroupService.findAll();
		String json = "[";
		for(UserGroup usergroup:ls){
			json +="{'id':"+usergroup.getId()+",";
			json +="'name':'"+usergroup.getName()+"'},";
		}
		//去除尾部逗号
		if(ls.size()>0){
			json = json.substring(0,json.length()-1);
		}
		json += "]";
		this.print(json);
	}
	
	public void getObjById(){
		RoleService roleService = (RoleService)this.getBean("roleService");	
		int id=StringUtil.parseInt(request.getParameter("id"));
		Role role = roleService.load(id);
		this.print(JsonUtil.obj2Json(role));
	}
	
	public void getUpRoles(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		RoleService roleService = (RoleService)this.getBean("roleService");	
		//int id = StringUtil.parseInt(this.request.getParameter("id"));
		List<Role> treels = roleService.findRootByGroupId(groupId);
		//List<Tree> ls = new ArrayList<Tree>();
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		for(Role role:treels){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", role.getId());
			map.put("text", role.getName());
			List<Role> leafls = roleService.findByProperty(role.getId(), groupId);
			if(leafls.size()>0){
				map.put("state", "closed");
				map.put("children", this.getChildNode(leafls, groupId));
			}
			maps.add(map);
		}
		this.print(JsonUtil.list2Json(maps));
	}
	
	public List<Map<String,Object>> getChildNode(List<Role> treels,int groupId){
		RoleService roleService = (RoleService)this.getBean("roleService");	
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		for(Role role:treels){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", role.getId());
			map.put("text", role.getName());
			List<Role> leafls = roleService.findByProperty(role.getId(), groupId);
			if(leafls.size()>0){
				map.put("state", "closed");
				map.put("children", this.getChildNode(leafls, groupId));
			}
			maps.add(map);
		}
		return maps;
	}
	
	
}
